//
//  CZEmoticonToolbar.swift
//  CZ29VVeibo
//
//  Created by apple on 16/7/4.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
// buttontag中间值
private let tagNum = 1000
// 表情键盘类型没记
enum CZEmoticonToolbarType:Int{
    // 默认
    case Default = 0
    // Emoji表情
    case Emoji = 1
    // 浪小花
    case Lxh = 2
}

class CZEmoticonToolbar: UIStackView {
    
    var currentButton: UIButton?
    
    // 声明一个闭包告知外界哪个按钮点击
    var toolbarClosure:((type: CZEmoticonToolbarType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 供外界调用方法
    func setCurrentButtons(tag: Int){
        // 通过tag获取button
        let button = viewWithTag(tag + tagNum) as! UIButton

        // 如果当前的buton等于我们选中的button直接返回
        if currentButton == button{
            return
        }
        
        /*
        01-  传入的button 变为选中状态
        02 - currentButton 变为正常状态
        03 - currentButton == 传入的button
        */
        button.selected = true
        currentButton?.selected = false
        currentButton = button
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        // 排列方式
        axis = .Horizontal
        // 填充方式(子控件大小相等 填充父控件)
        distribution = .FillEqually
        backgroundColor = RandomColor()
        let defaultButton = addChildButtons("left", title: "默认",type: .Default)
        addChildButtons("mid", title: "Emoji",type: .Emoji)
        addChildButtons("right", title: "浪小花",type: .Lxh)
//        // 添加约束
//        defaultButton.snp_makeConstraints { (make) -> Void in
//            make.top.leading.bottom.equalTo(self)
//            make.width.equalTo(emojiButton)
//        }
//        
//        emojiButton.snp_makeConstraints { (make) -> Void in
//            make.top.bottom.equalTo(self)
//            make.leading.equalTo(defaultButton.snp_trailing)
//            make.width.equalTo(lxhButton)
//        }
//        
//        lxhButton.snp_makeConstraints { (make) -> Void in
//            make.top.bottom.trailing.equalTo(self)
//            make.leading.equalTo(emojiButton.snp_trailing)
//        }
        // 首次默认按钮为选中状态
        defaultButton.selected = true
        currentButton = defaultButton
        
    }
    // MARK: - 监听方法
    func buttonClick(button: UIButton){
        // 如果当前的buton等于我们选中的button直接返回
        if currentButton == button{
            return
        }
        
        /*
            01-  传入的button 变为选中状态
            02 - currentButton 变为正常状态
            03 - currentButton == 传入的button
        */
        button.selected = true
        currentButton?.selected = false
        currentButton = button
        // 执行闭包
        toolbarClosure?(type: CZEmoticonToolbarType(rawValue: button.tag - tagNum)!)
    }
    
    // 创建button的公共方法
    // compose_emotion_table_left_normal
    //compose_emotion_table_left_selected
    func addChildButtons(imgName: String, title: String,type: CZEmoticonToolbarType) -> UIButton{
        let button = UIButton()
        // 设置tag
        button.tag = type.rawValue + tagNum
        // 添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_selected"), forState: UIControlState.Selected)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        // 添加button
        //addSubview(button)
        self.addArrangedSubview(button)
        return button
    }
    

}
